/**
 * MIT License
 * 
 * Copyright (c) 2021 CaiDongyu
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.axe.spring.cloud.configer;

import org.axe.helper.base.ConfigHelper;
import org.axe.helper.ioc.ClassHelper;
import org.axe.spring.cloud.annotation.EnableAxe;
import org.axe.util.AxeProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;

public class AxeSpringConfiger implements ImportBeanDefinitionRegistrar{
	 private static final Logger LOGGER = LoggerFactory.getLogger(AxeSpringConfiger.class);

	 public static Boolean AXE_CAN_INIT = false;
	 
	 @Override
	  public void registerBeanDefinitions(AnnotationMetadata am, BeanDefinitionRegistry registry) {
	  		try {
	  			//first step set axe config
	  			StandardAnnotationMetadata sam = (StandardAnnotationMetadata)am;
	  			
	  			AnnotationAttributes attributes = AnnotationAttributes.fromMap(sam.getAnnotationAttributes(EnableAxe.class.getName()));
	  			if(attributes == null) {
	  				return;
	  			}
	  			
	  			synchronized (AXE_CAN_INIT) {
	  				AXE_CAN_INIT = true;
				}
	  			
	  			//thread unsafe
	  			ClassHelper.addClass(sam.getIntrospectedClass());
	  			AxeProperties.init(null);
	 			ConfigHelper.setConfigProps(AxeProperties.getPROPERTIES());
	 			
	 			String[] url_match = attributes.getStringArray("url_match");
	 			AxeDispatcherFilter.cross_origin = attributes.getBoolean("cross_origin");
				if(url_match.length > 0) {
					AxeDispatcherFilter.urlMatch = url_match;
				}
	  			LOGGER.info("config axe success");
	  		} catch (Exception e) {
	  			LOGGER.error(e.getMessage(), e);
	  			System.exit(0);
	  		}
	  }
	 
	 
}